package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

//攻略明细控制器
@Controller
@RequestMapping("strategy")
public class StrategyController {
    //注入攻略明细业务
    @Autowired
    private IStrategyService strategyService;
    //主题服务
    @Autowired
    private IStrategyThemeService strategyThemeService;
    //攻略明细分页查询
    @RequestMapping("/list")
    public String list(Model model , @ModelAttribute("qo") StrategyQuery qo){
        //Page是spring-data-mongodb 里封装分页参数的对象.类似pegeinfo
        Page<Strategy> page = strategyService.list(qo);
        model.addAttribute("page" , page);
        return "strategy/list";
    }

    //新增/编辑
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy){
        if (StringUtils.hasLength(strategy.getId())){
            //带id则是编辑
            strategyService.update(strategy);
        }else {
            //没带则是新增
            strategyService.save(strategy);
        }
        //返回结果状态
        return JsonResult.success();
    }

    //删除
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        strategyService.delete(id);
        //返回结果状态
        return JsonResult.success();
    }

    //跳转新增编辑页面
    @RequestMapping("/input")
    public String input(Model model , @ModelAttribute("id")String id){
        //catalogs
        List<CatalogVO> catalogs = strategyService.groupList();
        model.addAttribute("catalogs" , catalogs);
        //themes
        model.addAttribute("themes" , strategyThemeService.allList());
        //给编辑回显数据
        if (StringUtils.hasLength(id)){
            model.addAttribute("strategy" ,strategyService.get(id));
        }
        return "strategy/input";
    }


    //下架
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(String id , String state){
        strategyService.changeState(id , state);
        //返回结果状态
        return JsonResult.success();
    }

}
